#include "Speedhack.h"

Speedhack g_pSpeedhack;
QueryPerformanceCounter_t QPC_Orig;

void Speedhack::Init()
{
	QPC_Orig = (QueryPerformanceCounter_t) SD_Tools::DetourFunc((BYTE*) GetProcAddress(GetModuleHandleW(L"kernel32.dll"),"QueryPerformanceCounter"), (BYTE*) QPC_Hook, 5);
	Active = false;
	Multiplier = 5.0f;
	OldReal = 0;
	OldFake = 0;
}

void Speedhack::Enable()
{
	Active = true;
}

void Speedhack::Disable()
{
	Active = false;
}

void Speedhack::Set( bool Active )
{
	this->Active = Active;
}

void Speedhack::SetMultiplier( float Multiplier )
{
	this->Multiplier = Multiplier;
}

void Speedhack::ComputeFakeCount( LARGE_INTEGER* Count )
{
	LONGLONG NewReal;
	LONGLONG NewFake;
	if (!OldFake || !OldReal)
	{
		OldFake = Count->QuadPart;
		OldReal = Count->QuadPart;
	}
	NewReal = Count->QuadPart;
	if (Active)
	{
		NewFake = OldFake + (LONGLONG)((NewReal-OldReal)*Multiplier);
	}
	else
	{
		NewFake = OldFake + (LONGLONG)(NewReal-OldReal);
	}
	OldFake = NewFake;
	OldReal = Count->QuadPart;
	Count->QuadPart = NewFake;
}

BOOL __stdcall QPC_Hook( LARGE_INTEGER* Counter )
{
	BOOL Rslt = QPC_Orig(Counter);
	g_pSpeedhack.ComputeFakeCount(Counter);
	return Rslt;
}
